package com.coreservlets.jsf.ss12;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

@ManagedBean
@SessionScoped
/**
 * From <a href="http://courses.coreservlets.com/Course-Materials/"> the courservlets.com tutorials on servlets, JSP, Struts, JSF, Ajax, 
 * GWT, Spring, Hibernate/JPA, and Java Programming</a>
 * @author InfZero
 *
 */
public class LocationBean implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private String state, city;
	private boolean isCityListDisabled = true;
	
	public String getState() 
	{
		return( state );
	}
	
	public void setState(String state) 
	{
		this.state = state;
		isCityListDisabled = false;
	}
	
	public String getCity() 
	{
		return( city );
	}
	
	public void setCity(String city) 
	{
		this.city = city;
	}
	
	public boolean isCityListDisabled() 
	{
		return isCityListDisabled;
	}
	
	public List<SelectItem> getStates()
	{
		List<SelectItem> states = new ArrayList< SelectItem >();
		
		states.add( new SelectItem( "--- Select State ---" ) );
		
		for( StateInfo stateData : StateInfo.getNearbySates() )
		{
			states.add( new SelectItem( stateData.getStateName() ) );
		}
		
		return( states );
	}
	
	public SelectItem[] getCities()
	{
		SelectItem[] cities = { new SelectItem( "--- Choose City ---") };
		
		if( !isCityListDisabled && ( state != null ))
		{
			for( StateInfo stateData : StateInfo.getNearbySates() )
			{
				if( state.equals( stateData.getStateName()))
				{
					cities = stateData.getCities();
					break;
				}
			}
		}
		
		return( cities );
	}
}
